package com.ukao.pad.ui.setting;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.pad.R;
import com.ukao.pad.adapter.StationAdapter;
import com.ukao.pad.base.MvpFragment;
import com.ukao.pad.bean.StationBean;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.eventbus.MainEvent;
import com.ukao.pad.listener.MyOnItemClickListener;
import com.ukao.pad.presenter.StationPesenter;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.T;
import com.ukao.pad.view.StationView;
import com.ukao.pad.widget.MultipleStatusView;
import com.ukao.pad.widget.MyLRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.ukao.pad.eventbus.MainEvent.Message.station;


public class StationFragment extends MvpFragment<StationPesenter>  implements StationView {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;
    private String mParam1;
    private String mParam2;
    private StationAdapter mStationAdapter;
    private MultipleStatusView statusview;
    private MyLRecyclerView recyclerView;
    private boolean isLoadSucceed;
    private ArrayList<StationBean> mStationData;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    public StationBean mStationBean;


    public static StationFragment newInstance(boolean  putawayMod, String param2) {
        StationFragment fragment = new StationFragment();
        Bundle args = new Bundle();
        args.putBoolean(ARG_PARAM1, putawayMod);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
           boolean putawayMod= getArguments().getBoolean(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected StationPesenter createPresenter() {
        return new StationPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_station, container, false);
        this.statusview = (MultipleStatusView) view.findViewById(R.id.status_view);
        this.recyclerView = (MyLRecyclerView) view.findViewById(R.id.lurecycler_view);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!isLoadSucceed){
            recyclerView.forceToRefresh();
        }
    }

    @Override
    protected void initView() {
         mStationData= new ArrayList<>();
        initLinearRecyclerView(recyclerView);
        mStationAdapter = new StationAdapter(_mActivity,mStationData);
        mLRecyclerViewAdapter= new LRecyclerViewAdapter(mStationAdapter);
        recyclerView.setAdapter(mLRecyclerViewAdapter);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mStationAdapter.setOnItemClickListener(mOnItemClick);
        recyclerView.setOnRefreshListener(mOnRefreshListener);
        statusview.setOnRetryClickListener(mOnRetryClick);
    }
    private OnRefreshListener  mOnRefreshListener=new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mStationAdapter.getDatas().clear();
            mLRecyclerViewAdapter.notifyDataSetChanged();
            mvpPresenter. stationTypesRequest();




        }
    };

    private OnItemClickListener  mOnItemClick=new MyOnItemClickListener(){
        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);
            if (mStationAdapter.getItem(i)==null)return;
            StationBean mitem = mStationAdapter.getItem(i);
            if (!mitem.isCheck()) {
                mvpPresenter.changeOperType(mitem.getId(), i);


            }

        }
    };



    private View.OnClickListener   mOnRetryClick=new  View.OnClickListener(){
        @Override
        public void onClick(View v) {

           mvpPresenter.stationTypesRequest();
        }
    };








    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void loadStationSuccess(List<StationBean> mAddressList) {

       if(mAddressList.size() == 0) {
            statusview.showEmpty();
        } else {
           isLoadSucceed=true;
            statusview.showContent();
            mStationAdapter.setDatas(mAddressList);
            recyclerView.refreshComplete(10);
            mLRecyclerViewAdapter.notifyDataSetChanged();

        }
    }

    @Override
    public void showNoNetwork() {
        statusview.showNoNetwork();
    }

    @Override
    public void changeOperTypeSucceed(int pos) {

        if (CheckUtils.isNull(mStationAdapter.getItem(pos)))return;
        StationBean mitem =mStationAdapter.getItem(pos) ;
        mitem.setCheck(!mitem.isCheck());

        for (int j = 0; j < mStationAdapter.getDatas().size(); j++) {
            if (pos!= j) {
                mStationAdapter.getDatas().get(j).setCheck(false);

            }
        }

        mLRecyclerViewAdapter.notifyDataSetChanged();//必须调用此方法
    }

    @Override
    public void loadFail(String errs) {
    T.show(errs);
    }
}
